package com.jieli.stream.dv.running2.ui.base;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.jieli.stream.dv.running2.R;


public class BaseDialogFragment extends DialogFragment {
    private boolean isShown = false;

    private Bundle bundle;

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        if(window == null) return;
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.dimAmount = 0.5f;
        windowParams.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        windowParams.windowAnimations = R.style.MyDialogTheme;
        window.setAttributes(windowParams);
    }

    @Override
    public void show(FragmentManager manager, String tag) {
//        super.show(manager, tag);
        if(isShowing()){
            return;
        }
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commitAllowingStateLoss();

        isShown = true;
    }

    @Override
    public void dismiss() {
        //if (getActivity() != null)
        dismissAllowingStateLoss();
        isShown = false;
    }

    public boolean isShowing() {
        return isShown;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (getActivity() != null) super.onDismiss(dialog);
        isShown = false;
    }

    public Bundle getBundle() {
        return bundle;
    }

    public void setBundle(Bundle bundle) {
        this.bundle = bundle;
    }
}
